package info.wf0913.blogger_core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import info.wf0913.blogger_core.bean.BaseEntity;
import info.wf0913.blogger_core.bean.Result;
import info.wf0913.blogger_core.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;
import java.util.List;

public class BaseController<S extends BaseService<T>,T extends BaseEntity> {
    @Autowired
    protected S service ;

    /**
     * 分页查询数据
     * @param page 页数
     * @param limit 条数
     * @param entity 分页的类
     * @return
     */
    @PostMapping("/listPage")
    public Result listPage(@RequestParam(name = "page", defaultValue = "1", required = false) int page,
                           @RequestParam(name = "rows", defaultValue = "10", required = false) int limit,
                           T entity)
    {
        try {
            return Result.ok().setData(service.listPage(page,limit,entity));
        }catch (Exception e){
            return Result.failure() ;
        }
    }

    /**
     * 查询全部信息
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        try {
            return Result.ok().setData(service.list()) ;
        }catch (Exception e){
            return Result.failure() ;
        }
    }
    @PostMapping("/update")
    public Result update(T entity){
        try {
            return Result.ok().setData(service.saveOrUpdate(entity)) ;
        }catch (Exception e){
            return Result.failure() ;
        }
    }

    /**
     * 单删除一个
     * @param id 需要删除的id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        try {
            return Result.ok().setData(service.removeById(id)) ;
        }catch (Exception e){
            return Result.failure() ;
        }
    }

    /**
     * 一次删除多个
     * @param listId
     * @return
     */
    @GetMapping("deleteList")
    public Result deleteList(@RequestParam List<String> listId){
        try {
            return Result.ok().setData(service.removeByIds(listId));
        }catch (Exception e){
            return Result.failure() ;
        }
    }


}
